package flc.ast.activity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityCompassBinding;

public class CompassActivity extends BaseAc<ActivityCompassBinding> implements SensorEventListener {

    private float lastRotateDegree;
    private SensorManager sensorManager;
    private long lastUpdateTime = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_compass;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCompassBack.setOnClickListener(v -> onBackPressed());

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        //加速度感应器
        Sensor magneticSensor = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
        //地磁感应器
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this, magneticSensor, SensorManager.SENSOR_DELAY_GAME);
        sensorManager.registerListener(this, accelerometerSensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void initData() {

    }

    float[] accelerometerValues = new float[3];

    float[] magneticValues = new float[3];

    @Override
    public void onSensorChanged(SensorEvent event) {
        // 判断当前是加速度感应器还是地磁感应器
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            //赋值调用clone方法
            accelerometerValues = event.values.clone();
        } else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            //赋值调用clone方法
            magneticValues = event.values.clone();
        }
        if (accelerometerValues != null && magneticValues != null) {
            long curTime = System.currentTimeMillis();
            if (curTime - lastUpdateTime < 500) return;
            lastUpdateTime = curTime;
            calculateOrientation(); // 加速度和磁场强度两个都有了，才能计算磁极的方向
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    //计算方位
    private void calculateOrientation() {
        float[] R = new float[9];
        float[] values = new float[3];
        SensorManager.getRotationMatrix(R, null, accelerometerValues, magneticValues);
        sensorManager.getOrientation(R, values);
        //values[0]的取值范围是-180到180度。
        double value = Math.toDegrees(values[0]);
        //+-180表示正南方向，0度表示正北，-90表示正西，+90表示正东
        if ((value >= 170 && value <= 180) || value >= -180 && value < -170) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text1);
        } else if (value >= 80 && value <= 100) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text2);
        } else if (value >= 100 && value < 170) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text3);
        } else if (value >= 10 && value < 80) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text4);
        } else if (value >= -100 && value < -80) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text5);
        } else if (value >= -170 && value < -100) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text6);
        } else if (value >= -80 && value < -10) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text7);
        } else if (value >= -10 && value < 10) {
            mDataBinding.tvCompassFx.setText(flc.ast.R.string.compass_text8);
        }
        mDataBinding.tvCompassJd.setText(String.format("%.1f", value < 0 ? value + 360 : value) + getString(flc.ast.R.string.unit_angle));
        //将计算出的旋转角度取反，用于旋转指南针背景图
        float rotateDegree = -(float) value;
        if (Math.abs(rotateDegree - lastRotateDegree) > 1) {
            RotateAnimation animation = new RotateAnimation(lastRotateDegree, rotateDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            animation.setFillAfter(true);
            mDataBinding.ivCompassZz.startAnimation(animation); //动画效果转动传感器
            lastRotateDegree = rotateDegree;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (sensorManager != null) {
            sensorManager.unregisterListener(this);
        }
    }

}